package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        //1.先获取登录人id
        Long userId = SecurityUtils.getUserId();
        //2.只有负责该老人的护理员才能看到报警数据
        alertData.setUserId(userId);
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return alertDataMapper.deleteById(id);
    }

    /**
     * 处理设备报警数据
     *
     * @param alertData 报警数据
     */
    @Override
    public void handleAlertData(AlertData alertData) {
        //获取登录人id，判断是否为空，为空直接结束
        Long userId = SecurityUtils.getUserId();
        if (ObjectUtil.isEmpty(userId)){
            throw new BaseException("登录人id错误");
        }

        //根据报警数据id获得整条报警数据信息
        AlertData data = getById(alertData.getId());

        //根据物联网id、功能id、报警规则id、产品key、创建时间获取整条数据信息
        LambdaQueryWrapper<AlertData> queryWrapper = Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getIotId, data.getIotId())
                .eq(AlertData::getFunctionId, data.getFunctionId())
                .eq(AlertData::getProductKey, data.getProductKey())
                .eq(AlertData::getAlertRuleId, data.getAlertRuleId());

        //根据这条报警数据查询到同一时间报警的全部数据
        List<AlertData> alertDataList = alertDataMapper.selectList(queryWrapper);

        //如果不为空，设置处理人、处理时间、处理结果、状态设置为1（已处理）
        alertDataList.forEach(dataAlert -> {
            dataAlert.setProcessingTime(alertData.getProcessingTime());
            dataAlert.setProcessingResult(alertData.getProcessingResult());
            dataAlert.setProcessorId(userId);
            dataAlert.setProcessorName(SecurityUtils.getUsername());
            dataAlert.setStatus(1);
        });

        //批量修改数据信息
        updateBatchById(alertDataList);
    }

}
